package com.app.lcl.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.app.lcl.entities.Address;
import com.app.lcl.entities.News;
import com.app.lcl.entities.Woman;

public class testJunit2 {
	
	private SessionFactory sf = null;
	private Session session = null;
	private Transaction tx = null;
	/**
	 * 测试前
	 * @throws Exception
	 */
	@Before
	public void setUp() throws Exception{
		System.out.println("----初始化数据----");
		Configuration conf = new Configuration().configure();
		StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder().applySettings(conf.getProperties());
		StandardServiceRegistry sr = srb.build();
		sf = conf.buildSessionFactory(sr);
		session = sf.openSession();
	}
	/**
	 * 测试后
	 * @throws Exception
	 */
	@After
	public void tearDown() throws Exception{
		System.out.println("----释放数据----");
		if(session.isOpen()){
			session.close();
		}
	}
	
	@Test
	public void testSave(){
		Transaction tx=session.beginTransaction();
		News n = new News();
		//n.setId(6);
		n.setTitle("新闻标题1");
		n.setContent("新闻内容xxxx");
		session.save(n);//insert obj
		tx.commit();//提交对象
	}
	@Test
	/**
	 * session缓存
	 */
	public void testSession(){
		News n = (News)session.get(News.class, 1);
		System.out.println(n);
		System.out.println("==================================================");
		//session.clear();
		News n1 = (News)session.get(News.class, 1);
		System.out.println(n1);
	}
	
	@Test
	public void testSession2(){
		tx = session.beginTransaction();
		for (int i = 0;i < 20;i++){
			News n = new News();
			n.setTitle("hsajj"+i);
			n.setContent("jdashkjdhajk"+i+i);
			session.persist(n);
			if(i % 10 == 0){
				session.flush();//立刻同步到数据库
				session.clear();//不需要缓存 时应尽快清空，防止浪费资源
			}
			News n1 = (News)session.get(News.class, 109+i);
			System.out.println(n1);
			System.out.println("============================");
		}
		tx.commit();
	}
	
	@Test
	public void testSaveAddress(){
		tx = session.beginTransaction();
		Address address = new Address(211100, "南京江宁河海大学");
		session.persist(address);
		tx.commit();
	}
	
	@Test
	public void testOneToOne2(){//2代表双向关联
		Woman woman = new Woman("老吴", "666666", new Date());
		Address address = new Address(211100,"河海大学");
		woman.setAddress(address);
		address.setWoman(woman);
		
		tx = session.beginTransaction();
		/*先存主表*/
		session.persist(woman);
		session.persist(address);
		tx.commit();
	}
	
	@Test
	public void testGetByMaster(){
		Woman woman = (Woman) session.get(Woman.class, 1);
		System.out.println(woman);
		System.out.println("==========================");
		System.out.println(woman.getAddress());
	}
	
	@Test
	public void testGetByWater(){
		Address address = (Address) session.get(Address.class, 1);
		System.out.println(address);
		System.out.println("==========================");
		System.out.println(address.getWoman());
	}
}
